<template>
  <!-- category（分类）信息管理页 -->
  <div class="main">
    <!-- 分类信息列表渲染 -->
    <el-table
      :data="categorys"
      style="width: 1002px; margin: 0 auto"
      max-height="352"
      stripe
      border
      :header-cell-style="headerStyle"
    >
      <!-- ID项 -->
      <el-table-column prop="categoryId" label="ID" width="300">
      </el-table-column>
      <!-- 名称项 -->
      <el-table-column label="名称" width="300">
        <template slot-scope="scope">
          <!-- 名称输入框 -->
          <div class="table_name_input">
            <el-input
              v-model="scope.row.categoryName"
              v-show="scope.row.show"
              size="small"
            >
            </el-input>
          </div>
          <!-- 名称展示文本框 -->
          <div class="table_name_text" v-show="!scope.row.show">
            {{ scope.row.categoryName }}
          </div>
        </template>
      </el-table-column>
      <!-- 操作栏 -->
      <el-table-column label="操作" width="400">
        <template slot-scope="scope">
          <!-- 编辑按钮 -->
          <el-button
            type="primary"
            @click="editCategoryName(scope.row)"
            icon="el-icon-edit"
            size="small"
          ></el-button>
          <!-- 编辑确认保存按钮 -->
          <el-button
            v-show="scope.row.show"
            type="success"
            @click="saveCategoryName(scope.row, scope.$index)"
            icon="el-icon-check"
            size="small"
          ></el-button>
          <!-- 编辑取消按钮 -->
          <el-button
            v-show="scope.row.show"
            type="info"
            @click="closeEditCategory(scope.row)"
            icon="el-icon-close"
            size="small"
          ></el-button>
          <!-- 删除按钮 -->
          <el-button
            @click="deleteRow(scope.$index, categorys)"
            size="small"
            type="primary"
            icon="el-icon-delete"
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加分类按钮 -->
    <el-button
      @click="addDialogFlag = true"
      class="addButton"
      size="medium"
      type="primary"
      round
      style="margin-bottom: 10px"
      >添加分类</el-button
    >
    <!-- 添加提示输入名称dialog -->
    <el-dialog
      title="请输入要添加的分类名称："
      :visible.sync="addDialogFlag"
      width="30%"
      center
      top="30vh"
    >
      <el-input v-model="CategoryNameWillBeAdd" size="small"> </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogFlag = false">取 消</el-button>
        <el-button type="primary" @click="addCatagory()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      categorys: [],
      isInputName: false,
      addDialogFlag: false,
      CategoryNameWillBeAdd: "",
    };
  },
  mounted() {
    this.getCategorys();
  },
  methods: {
    headerStyle() {
      return "color: #000;background-color: #c5464a;height:60px;";
    },
    getCategorys() {
      this.$http.get("jw-category/findAll").then((response) => {
        this.categorys = response.data.result;
      });
    },
    deleteRow(index, data) {
      const id = data[index].categoryId;
      const name = data[index].categoryName;
      this.$confirm(
        "此操作将永久删除ID为【" +
          id +
          "】，名称为【" +
          name +
          "】的项, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          showCancelButton: true,
          type: "warning",
        }
      )
        .then(() => {
          data.splice(index, 1);
          this.categorys = data;
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          // 删除数据库中的对应项
          this.$http
            .delete("jw-category/deleteById", {
              params: {
                id: id,
              },
            })
            .then((response) => {
              this.getCategorys();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    editCategoryName(row) {
      let flag = 1;
      this.categorys.forEach((e) => {
        if (e.show == true) {
          flag = 0;
        }
      });
      if (flag === 1) {
        row.show = true;
        console.log(row.show);
      }
    },
    saveCategoryName(row, index) {
      this.$confirm(
        "此操作将修改并保存ID为【" +
          row.categoryId +
          "】，名称为【" +
          row.categoryName +
          "】的项, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          showCancelButton: true,
          type: "warning",
        }
      )
        .then(() => {
          // 保存数据库中的对应项
          let category = row;
          // delete category.show;
          this.$http
            .post("jw-category/saveCategory", category)
            .then((reponse) => {
              this.$message({
                type: "success",
                message: "修改成功!",
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消修改",
          });
        });
      row.show = false;
    },
    closeEditCategory(row) {
      row.show = false;
      this.$message({
        type: "info",
        message: "已取消修改",
      });
    },
    addCatagory() {
      this.addDialogFlag = false;
      let category = {};
      category.categoryName = this.CategoryNameWillBeAdd;
      category.categoryId = -1;
      this.$http.post("jw-category/saveCategory", category).then((reponse) => {
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        // 重新获取列表数据
        this.getCategorys();
      });
    },
  },
};
</script>

<style scope>
.main {
  overflow: hidden;
}
.el-input__inner {
  padding: 0 !important;
}
.addButton {
  margin-top: 12px !important;
  float: right;
  right: 30px;
}
</style>
